package net.sea.setup.ui.wizard.impl.cfg.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import org.apache.commons.net.ftp.FTPClient;

import net.sea.commons.constants.AppConstants;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.config.custom.CustomConfigItem;
import net.sea.setup.ui.wizard.IWizardPage.PageResult;
import net.sea.setup.ui.wizard.impl.cfg.AbstractCfgPanel;

/**
 * 
 * 执行机配置页面
 *
 * @author huachengwu 
 */
public class EMCfgPanel extends AbstractCfgPanel {
	public EMCfgPanel() {
		create();
	}

	private static final long serialVersionUID = -4955232375865523937L;

	private JTextField ftpUserText;
	private JPasswordField ftpPwdText;
	private JTextField ftpIpText;
	private JTextField ftpPortText;
	private JTextField caseFloderText;

	@Override
	public PageResult validateInput() {
		// 验证IP
		String ip = ftpIpText.getText();
		String port = ftpPortText.getText();
		String user = ftpUserText.getText();
		String pwd = new String(ftpPwdText.getPassword());
		if (!isIP(ip)) {
			return new PageResult(false, "无效的IP地址");
		}
		// 验证端口
		if (!isPort(port)) {
			return new PageResult(false, "无效的端口号");
		}
		// 验证ftp用户
		// if (StringUtils.isBlank(user)) {
		// return new PageResult(false, "请输入FTP服务器用户");
		// }
		// // 验证ftp密码
		// if (StringUtils.isBlank(pwd)) {
		// return new PageResult(false,"请输入FTP服务器密码");
		// }
		// 验证FTP是否有效
		if (!isVaildFtp(ip, port, user, pwd)) {
			return new PageResult(false, "无效的FTP服务器");
		}
		return new PageResult(true);
	}

	/**
	 * 验证是否为可用FTP服务器
	 * 
	 * @param ip
	 *            IP地址
	 * @param port
	 *            端口
	 * @param user
	 *            用户
	 * @param pwd
	 *            密码
	 * @return true-可用FTP，false-不可用FTP
	 */
	private boolean isVaildFtp(String ip, String port, String user, String pwd) {
		FTPClient client = new FTPClient();
		try {
			client.connect(ip, Integer.parseInt(port));
			client.login(user, pwd);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 创建页面
	 */
	@Override
	protected void create() {
		setBorder(new TitledBorder(null, "\u6267\u884C\u673A\u914D\u7F6E",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagLayout gbl_emCfgPanel = new GridBagLayout();
		gbl_emCfgPanel.columnWidths = new int[] { 10, 0, 0, 0, 0, 10, 0 };
		gbl_emCfgPanel.rowHeights = new int[] { 10, 0, 0, 0, 0, 10, 0 };
		gbl_emCfgPanel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 1.0,
				0.0, Double.MIN_VALUE };
		gbl_emCfgPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		setLayout(gbl_emCfgPanel);

		JLabel ftpIpLb = new JLabel("FTP\u670D\u52A1\u5668\u5730\u5740\uFF1A");
		GridBagConstraints gbc_ftpIpLb = new GridBagConstraints();
		gbc_ftpIpLb.anchor = GridBagConstraints.EAST;
		gbc_ftpIpLb.insets = new Insets(0, 0, 5, 5);
		gbc_ftpIpLb.gridx = 1;
		gbc_ftpIpLb.gridy = 1;
		add(ftpIpLb, gbc_ftpIpLb);

		ftpIpText = new JTextField();
		ftpIpText.setText("127.0.0.1");
		GridBagConstraints gbc_ftpIpText = new GridBagConstraints();
		gbc_ftpIpText.insets = new Insets(0, 0, 5, 5);
		gbc_ftpIpText.fill = GridBagConstraints.HORIZONTAL;
		gbc_ftpIpText.gridx = 2;
		gbc_ftpIpText.gridy = 1;
		add(ftpIpText, gbc_ftpIpText);
		ftpIpText.setColumns(10);

		JLabel ftpPortLb = new JLabel("FTP\u670D\u52A1\u5668\u7AEF\u53E3\uFF1A");
		GridBagConstraints gbc_ftpPortLb = new GridBagConstraints();
		gbc_ftpPortLb.anchor = GridBagConstraints.EAST;
		gbc_ftpPortLb.insets = new Insets(0, 0, 5, 5);
		gbc_ftpPortLb.gridx = 3;
		gbc_ftpPortLb.gridy = 1;
		add(ftpPortLb, gbc_ftpPortLb);

		ftpPortText = new JTextField();
		ftpPortText.setText("21");
		GridBagConstraints gbc_ftpPortText = new GridBagConstraints();
		gbc_ftpPortText.insets = new Insets(0, 0, 5, 5);
		gbc_ftpPortText.fill = GridBagConstraints.HORIZONTAL;
		gbc_ftpPortText.gridx = 4;
		gbc_ftpPortText.gridy = 1;
		add(ftpPortText, gbc_ftpPortText);
		ftpPortText.setColumns(10);

		JLabel ftpUserLb = new JLabel("FTP\u670D\u52A1\u5668\u7528\u6237\uFF1A");
		GridBagConstraints gbc_ftpUserLb = new GridBagConstraints();
		gbc_ftpUserLb.insets = new Insets(0, 0, 5, 5);
		gbc_ftpUserLb.anchor = GridBagConstraints.EAST;
		gbc_ftpUserLb.gridx = 1;
		gbc_ftpUserLb.gridy = 2;
		add(ftpUserLb, gbc_ftpUserLb);

		ftpUserText = new JTextField();
		GridBagConstraints gbc_ftpUserText = new GridBagConstraints();
		gbc_ftpUserText.insets = new Insets(0, 0, 5, 5);
		gbc_ftpUserText.fill = GridBagConstraints.HORIZONTAL;
		gbc_ftpUserText.gridx = 2;
		gbc_ftpUserText.gridy = 2;
		add(ftpUserText, gbc_ftpUserText);
		ftpUserText.setColumns(10);

		JLabel ftpPwdLb = new JLabel("FTP\u670D\u52A1\u5668\u5BC6\u7801\uFF1A");
		GridBagConstraints gbc_ftpPwdLb = new GridBagConstraints();
		gbc_ftpPwdLb.fill = GridBagConstraints.VERTICAL;
		gbc_ftpPwdLb.anchor = GridBagConstraints.EAST;
		gbc_ftpPwdLb.insets = new Insets(0, 0, 5, 5);
		gbc_ftpPwdLb.gridx = 3;
		gbc_ftpPwdLb.gridy = 2;
		add(ftpPwdLb, gbc_ftpPwdLb);

		ftpPwdText = new JPasswordField();
		GridBagConstraints gbc_ftpPwdText = new GridBagConstraints();
		gbc_ftpPwdText.insets = new Insets(0, 0, 5, 5);
		gbc_ftpPwdText.fill = GridBagConstraints.HORIZONTAL;
		gbc_ftpPwdText.gridx = 4;
		gbc_ftpPwdText.gridy = 2;
		add(ftpPwdText, gbc_ftpPwdText);
		ftpPwdText.setColumns(10);

		JLabel caseFolderLb = new JLabel(
				"\u6848\u4F8B\u4E0B\u53D1\u76EE\u5F55\uFF1A");
		GridBagConstraints gbc_caseFolderLb = new GridBagConstraints();
		gbc_caseFolderLb.anchor = GridBagConstraints.EAST;
		gbc_caseFolderLb.insets = new Insets(0, 0, 5, 5);
		gbc_caseFolderLb.gridx = 1;
		gbc_caseFolderLb.gridy = 3;
		add(caseFolderLb, gbc_caseFolderLb);

		caseFloderText = new JTextField();
		caseFloderText.setEditable(false);
		GridBagConstraints gbc_caseFloderText = new GridBagConstraints();
		gbc_caseFloderText.gridwidth = 2;
		gbc_caseFloderText.insets = new Insets(0, 0, 5, 5);
		gbc_caseFloderText.fill = GridBagConstraints.HORIZONTAL;
		gbc_caseFloderText.gridx = 2;
		gbc_caseFloderText.gridy = 3;
		add(caseFloderText, gbc_caseFloderText);
		caseFloderText.setColumns(10);

		JButton caseFloderBtn = new JButton("...");
		caseFloderBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectFolder(caseFloderText);
			}
		});
		GridBagConstraints gbc_caseFloderBtn = new GridBagConstraints();
		gbc_caseFloderBtn.insets = new Insets(0, 0, 5, 5);
		gbc_caseFloderBtn.gridx = 4;
		gbc_caseFloderBtn.gridy = 3;
		add(caseFloderBtn, gbc_caseFloderBtn);

		JButton ftpServerTestBtn = new JButton("\u6D4B\u8BD5FTP\u8FDE\u63A5");
		ftpServerTestBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String ip = ftpIpText.getText();
				String port = ftpPortText.getText();
				String user = ftpUserText.getText();
				String pwd = new String(ftpPwdText.getPassword());
				if (isVaildFtp(ip, port, user, pwd)) {
					JOptionPane.showMessageDialog(getParent(), "有效的FTP服务器");
				} else {
					JOptionPane.showMessageDialog(getParent(), "无效的FTP服务器");
				}
			}
		});
		GridBagConstraints gbc_ftpServerTestBtn = new GridBagConstraints();
		gbc_ftpServerTestBtn.insets = new Insets(0, 0, 5, 5);
		gbc_ftpServerTestBtn.gridx = 2;
		gbc_ftpServerTestBtn.gridy = 4;
		add(ftpServerTestBtn, gbc_ftpServerTestBtn);
	}

	@SuppressWarnings("unchecked")
	@Override
	public CustomConfig buildCfg() {
		String caseDownPath = caseFloderText.getText();
		String ftpIp = ftpIpText.getText();
		String ftpPort = ftpPortText.getText();
		String ftpUser = ftpUserText.getText();
		String ftpPwd = new String(ftpPwdText.getPassword());

		// 构建执行机自定义配置
		CustomConfig config = new CustomConfig();
		config.setType("执行机配置");

		CustomConfigItem ip = new CustomConfigItem();
		ip.setKey("FTP服务器IP");
		ip.setValue(ftpIp);
		config.addItem(ip);

		CustomConfigItem port = new CustomConfigItem();
		port.setKey("FTP服务器端口");
		port.setValue(ftpPort);
		config.addItem(port);

		CustomConfigItem user = new CustomConfigItem();
		user.setKey("FTP服务器用户");
		user.setValue(ftpUser);
		config.addItem(user);

		CustomConfigItem pwd = new CustomConfigItem();
		pwd.setKey("FTP服务器密码");
		pwd.setVisible(false);
		pwd.setValue(ftpPwd);
		config.addItem(pwd);

		CustomConfigItem caseDown = new CustomConfigItem();
		caseDown.setKey("案例下发目录");
		caseDown.setValue(caseDownPath);
		config.addItem(caseDown);

		// 保存配置系统到系统上下文
		Map<String, CustomConfig> customCfgMap = (Map<String, CustomConfig>) contextInst
				.getAttribute(AppConstants.CUSTOM_CONFIG_MAP_KEY);
		customCfgMap.put(this.getClass().getName(), config);

		// 应用上下文配置
		contextInst.setAttribute(AppConstants.EM_CASE_DOWN_DIR, caseDownPath);
		contextInst.setAttribute(AppConstants.EM_FTP_IP, ftpIp);
		contextInst.setAttribute(AppConstants.EM_FTP_PORT, ftpPort);
		contextInst.setAttribute(AppConstants.EM_FTP_USER, ftpUser);
		contextInst.setAttribute(AppConstants.EM_FTP_PWD, ftpPwd);

		return config;
	}
}
